package com.emser.unc.client.view;

import com.emser.unc.client.CloudMobile;
import com.emser.unc.client.presenter.SellerProductsPresenter;
import com.emser.unc.serializable.ProductDTO;
import com.google.gwt.cell.client.CheckboxCell;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.HasKeyboardSelectionPolicy.KeyboardSelectionPolicy;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.cellview.client.SimplePager.TextLocation;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.view.client.DefaultSelectionEventManager;
import com.google.gwt.view.client.HasData;
import com.google.gwt.view.client.MultiSelectionModel;
import com.google.gwt.view.client.ProvidesKey;
import com.google.gwt.view.client.Range;

public class SellerProductsListView extends Composite implements SellerProductsPresenter.Display{
	private Button btnGoBack;
	private Button btnSave;
	private SimplePager pager;
	private MultiSelectionModel<ProductDTO> selectionModel;
	private CellTable<ProductDTO> productsTable;
	private VerticalPanel verticalPanel;
	private int pageSize = CloudMobile.constants.pageSizeCP();
	private BusyIndicatorView busyIndicator = new BusyIndicatorView(CloudMobile.constants.msjWorking());;
	private ProvidesKey<ProductDTO> KEY_PROVIDER = new ProvidesKey<ProductDTO>() {
	      @Override
	      public Object getKey(ProductDTO item) {
	        return item == null ? null : item.getCode();
	      }
	    };
	private int hightSPanel;
	public SellerProductsListView() {
		verticalPanel = new VerticalPanel();
		initWidget(verticalPanel);
		verticalPanel.setSize("100%", CloudMobile.get().getHeightPanelCenter()+"px");
		hightSPanel = CloudMobile.get().getHeightPanelCenter() - 100;
		initHPanelTitle();
		initScrollPanel();
		initPager();
		busyIndicator.hide();
	}
	private void initScrollPanel() {
		ScrollPanel sPanel = new ScrollPanel();
		sPanel.setSize("100%-1px", hightSPanel+"px");
		productsTable = new CellTable<ProductDTO>(KEY_PROVIDER);
		productsTable.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.DISABLED);
		sPanel.add(productsTable);
		initColumnsTable();
		selectionModel = new MultiSelectionModel<ProductDTO>(KEY_PROVIDER);
	    productsTable.setSelectionModel(selectionModel, DefaultSelectionEventManager.<ProductDTO>createCheckboxManager());
		verticalPanel.add(sPanel);
		verticalPanel.setCellHorizontalAlignment(sPanel, HasHorizontalAlignment.ALIGN_CENTER);		
		verticalPanel.setCellHeight(sPanel, hightSPanel+"px");
		verticalPanel.setCellWidth(sPanel, "100%");
		productsTable.setSize("100%", "100%");
		
	}
	private void initHPanelTitle() {
		HorizontalPanel horizontalPanel = new HorizontalPanel();
		horizontalPanel.setStyleName("gwt-SimplePanelM1");
		verticalPanel.add(horizontalPanel);		
		verticalPanel.setCellHeight(horizontalPanel, "40px");
		verticalPanel.setCellWidth(horizontalPanel, "100%");
		horizontalPanel.setSize("100%", "40px");
		
		Grid grid = new Grid(1, 2);
		horizontalPanel.add(grid);
		horizontalPanel.setCellVerticalAlignment(grid, HasVerticalAlignment.ALIGN_MIDDLE);
		
		btnSave = new Button(CloudMobile.constants.save());
		grid.setWidget(0, 0, btnSave);
		btnSave.setSize("112", "28");
		
		btnGoBack = new Button(CloudMobile.constants.goBack());
		grid.setWidget(0, 1, btnGoBack);
		btnGoBack.setSize("112", "28");
		
		grid.getCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_LEFT);
	    grid.getCellFormatter().setHorizontalAlignment(0, 1, HasHorizontalAlignment.ALIGN_LEFT);
	}
	private void initPager() {
		SimplePanel simplePanel = new SimplePanel();
		simplePanel.setSize("400px", "40px");
		pager = new SimplePager(TextLocation.CENTER){
			@Override
	    	public void setPageStart(int index) {
	    	  if (getDisplay() != null) {
	    	    Range range = getDisplay().getVisibleRange();
	    	    int pageSize = range.getLength();
	    	    index = Math.max(0, index);
	    	    if (index != range.getStart()) {
	    	      getDisplay().setVisibleRange(index, pageSize);
	    	    }
	    	  }
	    	}
	    };
		pager.setStyleName("gwt-pager1");
	    pager.setStylePrimaryName("gwt-pager1");
		pager.setRangeLimited(true);
	    pager.setDisplay(productsTable);
	    pager.setSize("400px", "15px");
	    pager.setPageSize(pageSize);
	    pager.setPage(0);
		simplePanel.add(pager);
		verticalPanel.add(simplePanel);
		verticalPanel.setCellHorizontalAlignment(simplePanel, HasHorizontalAlignment.ALIGN_CENTER);
		verticalPanel.setCellVerticalAlignment(simplePanel, HasVerticalAlignment.ALIGN_MIDDLE);
		verticalPanel.setCellHeight(simplePanel, "40px");
		verticalPanel.setCellWidth(simplePanel, "100%");
		
	}
	private void initColumnsTable() {
		Column<ProductDTO, Boolean> chkboxColumn = new Column<ProductDTO, Boolean>(new CheckboxCell(true,false)) {
			@Override
			public Boolean getValue(ProductDTO object) {
				return selectionModel.isSelected(object);
			}
		};
		chkboxColumn.setVerticalAlignment(HasVerticalAlignment.ALIGN_TOP);
		productsTable.addColumn(chkboxColumn);
		
		TextColumn<ProductDTO> codeColumn = new TextColumn<ProductDTO>() {
			@Override
			public String getValue(ProductDTO object) {
				return object.getCode().toString();
			}
		};
		codeColumn.setVerticalAlignment(HasVerticalAlignment.ALIGN_TOP);
		productsTable.addColumn(codeColumn, CloudMobile.constants.code());
		TextColumn<ProductDTO> productsColumn = new TextColumn<ProductDTO>() {
			@Override
			public String getValue(ProductDTO object) {
				return object.getTitle();
			}
		};
		productsColumn.setVerticalAlignment(HasVerticalAlignment.ALIGN_TOP);
		productsTable.addColumn(productsColumn, CloudMobile.constants.products());
		
	}
	@Override
	public HasClickHandlers getSaveButton() {
		return btnSave;
	}
	
	@Override
	public MultiSelectionModel<ProductDTO> getSelectionModel() {
		return selectionModel;
	}
	@Override
	public HasData<ProductDTO> getTable() {
		return productsTable;
	}
	@Override
	public HasClickHandlers getGoBackButton() {
		return btnGoBack;
	}
	@Override
	public void enableBtnSave(boolean enabled) {
		btnSave.setEnabled(enabled);	
	}
	@Override
	public void showBusyIndicator(){
		busyIndicator.show();
	}
	@Override
	public void hideBusyIndicator(){
		busyIndicator.hide();
	}
	@Override
	public void setPage(int page) {
		pager.setPage(page);		
	}
	@Override
	public int getPage() {
		return pager.getPage();
	}

}
